/**
*
*/
package de.yaams.extensions.rgssproject.map.nevent.core;
import java.util.ArrayList;
import java.util.List;
import org.jruby.RubyBoolean;
import org.jruby.RubyFixnum;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.runtime.builtin.IRubyObject;
import de.yaams.extensions.jruby.RubyHelper;
import de.yaams.extensions.rgssproject.RGSSProjectHelper;
import de.yaams.extensions.rgssproject.database.RGSS1Helper;
import de.yaams.extensions.rgssproject.database.RGSS1Helper.Type;
import de.yaams.extensions.rgssproject.map.nevent.YEventCommandList;
import de.yaams.maker.helper.gui.form.FormComboBox;
import de.yaams.maker.helper.gui.form.core.FormBuilder;
import de.yaams.maker.helper.language.I18N;
/**
* @author abt
*
*/
public abstract class EventCommand {
protected int id;
protected boolean beta;
protected String[] titles, ids;
/**
* Create a new YBaseEventCodeManager
*/
public EventCommand() {
}
/**
* Get the title for the event command
*
* @param e
* @return
*/
public String getTitle(EventCode e) {
// has no cache?
if (e.getTitleCache() == null) {
e.setTitleCache(getInternTitle(e));
}
return e.getTitleCache();
}
/**
* Helpermethod to get the name of an element
*
* @param e
* @param index
* @param type
* @return
*/
protected String getTypeName(EventCode e, int index, Type type) {
return RGSS1Helper.get(e.getYecl().getProject(), type).get(RubyHelper.toInt(e.getParameters(), index)).getName();
}
/**
* Helpermethod to get the icon of an element
*
* @param e
* @param index
* @param type
* @return
*/
protected Object getTypeIcon(EventCode e, int index, Type type) {
return RGSS1Helper.getIcon(e.getYecl().getProject(), type, RubyHelper.toInt(e.getParameters(), index));
}
/**
* install Multiple ID Support
*
* @param titles
* @param ids
*/
protected void installMultipleIDSupport(String[] titles, String[] ids) {
this.titles = titles;
this.ids = ids;
}
/**
* Get the id switcher
*
* @param title
* @param e
* @return
*/
protected FormComboBox addIDSwichter(FormBuilder f, EventCode e) {
// add type
f.addElement("basic.type", CommandFormHelper.buildCodeIdChancer(e, I18N.t("Type"), ids, titles));
return (FormComboBox) f.getElement("basic.type");
}
/**
* Find the right title for the eventcode?
*
* @param e
* @return
*/
protected String getIDTitle(EventCode e) {
String id = Integer.toString(e.getId());
for (int i = 0, l = ids.length; i < l; i++) {
if (ids[i].equals(id)) {
return titles[i];
}
}
return "?" + Integer.toString(e.getId());
}
/**
* Get the title for the event command
*
* @param e
* @return
*/
protected abstract String getInternTitle(EventCode e);
/**
* Build from raw list the view list
*
* @param list
* @param os
* @param firstObject
*/
public void rawToView(ArrayList<EventCode> list, List<IRubyObject> os, RubyObject firstObject, YEventCommandList yecl) {
list.add(new EventCode(this, firstObject, yecl));
os.remove(0);
}
/**
* Build from view list the raw list
*
* @param list
* @param rawList
* @param firstObject
*/
public void viewToRaw(ArrayList<EventCode> list, List<Object> rawList, EventCode code) {
code.saveBack();
// remove the first object
rawList.add(code.getObject());
list.remove(0);
}
/**
* Create a new EventCode
*/
public void createNew(YEventCommandList yecl, int indent) {
// add it
createNew(yecl, indent, yecl.getList().getList().getSelectedIndex());
}
/**
* Create a new EventCode
*/
public void createNew(YEventCommandList yecl, int indent, int pos) {
// build it
EventCode e = new EventCode(this, (RubyObject) RGSSProjectHelper.getInterpreter(yecl.getProject()).runScriptlet(
"return RPG::EventCommand.new(" + id + "," + indent + ",[" + getStartParameter() + "])"), yecl);
// add it
createNew(yecl, e, pos);
}
/**
* Create a new EventCode
*/
public void createNew(YEventCommandList yecl, EventCode e, int pos) {
// add it
yecl.getList().add(pos, e);
yecl.saveBack();
}
/**
* Helpermethod to get the content of the start parameter array.
*
* @return
*/
protected abstract String getStartParameter();
/**
* Get the icon
*
* @return
*/
public abstract String getIcon();
/**
* Get the icon
*
* @return
*/
public abstract Object getIcon(EventCode e);
/**
* Get the translated name
*
* @return
*/
public abstract String getName();
/**
* Get the translated group
*
* @return
*/
public abstract String getGroup();
/**
* Build the panel for this element
*
* @return
*/
public abstract void buildPanel(FormBuilder f, EventCode e);
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* Helpermethod to add a missing boolean in the parameters
*
* @param EventCode
* @param index
* @param standard
* Value
*/
protected void addBoolean(final EventCode e, int index, boolean std) {
if (e.getParameters().size() <= index) {
e.getParameters().add(new RubyBoolean(e.getObject().getRuntime(), std));
}
}
/**
* Helpermethod to add a missing string in the parameters
*
* @param EventCode
* @param index
* @param standard
* Value
*/
protected void addString(final EventCode e, int index, String std) {
if (e.getParameters().size() <= index) {
e.getParameters().add(RubyString.newString(e.getObject().getRuntime(), std));
}
}
/**
* Helpermethod to add a missing integer in the parameters
*
* @param EventCode
* @param index
* @param standard
* Value
*/
protected void addInt(final EventCode e, int index, long std) {
if (e.getParameters().size() <= index) {
e.getParameters().add(RubyFixnum.one(e.getObject().getRuntime()));
// e.getParameters().add((IRubyObject)
// RGSSProjectHelper.getInterpreter(e.getYecl().getProject()).runScriptlet("return '0'.to_i"));
// e.getParameters().add(RubyFixnum.zero(e.getObject().getRuntime()));//
// .newFixnum(e.getObject().getRuntime(),
// std));
}
}
/**
* @return the beta
*/
public boolean isBeta() {
return beta;
}
/**
* Helpermethod to check, if can delete
*
* @param canDelete
* @return
*/
public boolean canDelete(boolean canDelete) {
return true;
}
}